﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class DaoTaoHocHam
    {
        public String ID;
        public String MaDaoTaoHocHam;
        public String MaTenHocHam;
        public DateTime NgayCap;
        public String CoQuanCap;
        public String QuyetDinhPhongHocHam;

        public HocHam HocHam
        {
            get
            {
                return new HocHam(MaTenHocHam);
            }
            set
            {
                HocHam = value;
            }
        }

        public DaoTaoHocHam(String _ID)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM DaoTaoHocHam " +
                "WHERE ID = '" + _ID + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                ID = _ID;
                MaDaoTaoHocHam = row["MaDaoTaoHocHam"].ToString();
                MaTenHocHam = row["MaTenHocHam"].ToString();
                NgayCap = row.Field<DateTime>("NgayCap");
                CoQuanCap = row["CoQuanCap"].ToString();
                QuyetDinhPhongHocHam = row["QuyetDinhPhongHocHam"].ToString();
            }
        }

        public DaoTaoHocHam(HocHam _HocHam,
            DateTime _NgayCap, String _CoQuanCap, String _QuyetDinhPhongHocHam)
        {
            ID = Database.getID("DaoTaoHocHam", "ID");
            MaDaoTaoHocHam = Database.getID("DaoTaoHocHam", "MaDaoTaoHocHam");
            MaTenHocHam = _HocHam.MaTenHocHam;
            NgayCap = _NgayCap;
            CoQuanCap = _CoQuanCap;
            QuyetDinhPhongHocHam = _QuyetDinhPhongHocHam;

            //object
            HocHam = _HocHam;
        }

        public Boolean Save()
        {
            QueryResult result;
            //Insert
            result = Database.shareDatabase.Query("INSERT INTO DaoTaoHocHam VALUES(" +
                "'" + ID + "', " +
                "'" + MaDaoTaoHocHam + "', " +
                "'" + MaTenHocHam + "', " +
                "'" + Database.sqlDateTime(NgayCap) + "', " +
                "N'" + CoQuanCap + "', " +
                "N'" + QuyetDinhPhongHocHam + "')");
            if (result.ReturnCode == 200)
                return true;
            else
                return false;
        }
        public Boolean Update()
        {
            QueryResult result;
            if (Database.isDupplicate("DaoTaoHocHam", "ID", ID))
            {
                //Update
                result = Database.shareDatabase.Query("UPDATE DaoTaoHocHam SET " +
                    "NgayCap = '" + Database.sqlDateTime(NgayCap) + "', " +
                    "CoQuanCap = N'" + CoQuanCap + "', " +
                    "QuyetDinhPhongHocHam = N'" + QuyetDinhPhongHocHam + "' WHERE ID = '" + ID + "'");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
            return false;
        }
        public Boolean Delete()
        {
            QueryResult result = Database.shareDatabase.Query("DELETE FROM DaoTaoHocHam " +
                "WHERE ID = '" + ID + "'");
            if (result.ReturnCode == 200)
                return true;
            else
                return false;
        }

        public static List<DaoTaoHocHam> getAllDaoTaoHocHam(String _MaDaoTaoHocHam)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM DaoTaoHocHam " +
                "WHERE MaDaoTaoHocHam = '" + _MaDaoTaoHocHam + "'");
            if (result.ReturnCode == 200)
            {
                List<DaoTaoHocHam> _result = new List<DaoTaoHocHam>();
                foreach (DataRow dtr in result.Table.Rows)
                    _result.Add(new DaoTaoHocHam(dtr["ID"].ToString()));
                return _result;
            }
            else
                return null;
        }
    }
}
